package cn.hlife.com.hlfhaemocyte.utils;

import java.io.UnsupportedEncodingException;
import java.nio.charset.Charset;
import java.text.SimpleDateFormat;
import java.util.Date;

import cn.hlife.com.bluetooth20_connect.lib.BluetoothService;
import cn.hlife.com.hlfhaemocyte.bean.MeasureEntity;

/**
 * Created by Administrator on 2017/2/22.
 */
public class BTPrintReport {
    private BluetoothService bluetoothService;
    private static final String cmd1 = "1B 61 01"; // 居中对齐
    private static final String cmd3 = "0A";// 换行
    private static final String cmd5 = "1D 21 11";// 2倍大小
    private static final String cmd6 = "1D 21 00";// 正常大小
    private static final String cmd7 = "1B 61 00";// 左对齐
//    private SimpleDateFormat dateFormat;

    public void setBluetoothService(BluetoothService bluetoothService) {
        this.bluetoothService = bluetoothService;
    }

    public interface BTPrintReportListener {
        /**
         * 打印完成
         */
        void onFinish();

        /**
         * 打印出错
         */
        void printError();
    }

    public void outputStringDatas(MeasureEntity measureEntity, BTPrintReportListener btPrintReportListener) {
//        dateFormat = new SimpleDateFormat("yy/MM/dd");


        write(hexStringToBytes(cmd1));
        write(hexStringToBytes(cmd5));
        PrintCharacters("测试数据");
//        write(hexStringToBytes(cmd3));
//        PrintCharacters("母乳分析报告");
//        write(hexStringToBytes(cmd3));
//        write(hexStringToBytes(cmd6));
//        PrintCharacters("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
//
//        write(hexStringToBytes(cmd7));
//        write(hexStringToBytes(cmd3));
//        PrintCharacters("时间:" + dateFormat.format(new Date(report.getDate())));
//        write(hexStringToBytes(cmd3));
//        PrintCharacters("编号:" + dangan.getBianhao());
//
//        write(hexStringToBytes(cmd3));
//        PrintCharacters(printTwoData("姓名:" + dangan.getName(), "年龄:" + dangan.getAge() + "岁"));
//        write(hexStringToBytes(cmd3));
//        PrintCharacters(printData("项目", "结果", "单位", "结论"));
//
//        write(hexStringToBytes(cmd3));
//
//        PrintCharacters(printData(report.getProject(), report.getResult() + "", report.getUnit(), report.getConclusion()));
//
//        write(hexStringToBytes(cmd3));
//        PrintCharacters(printOneData("参考：(" + report.getCkxx() + "-" + report.getCksx() + ")"));
//        write(hexStringToBytes(cmd3));
//        count++;
//        if (count == reports.size()) {
//            PrintCharacters("%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%");
//            write(hexStringToBytes(cmd1));
//            write(hexStringToBytes(cmd3));
//            PrintCharacters("检验员："
//                    + report.getIdentifier());
//
//            write(hexStringToBytes(cmd3));
//            PrintCharacters("检验结果只对本次样本负责");
//            write(hexStringToBytes(cmd3));
//            write(hexStringToBytes(cmd3));
//            btPrintReportListener.onFinish();


        }

    public void write(byte[] mes) {

        if (bluetoothService != null) {
            bluetoothService.write(mes);
        }

    }

    public void PrintCharacters(String str) {
        byte[] send;
        try {
            send = str.getBytes("GBK");
        } catch (UnsupportedEncodingException var4) {
            send = str.getBytes();
        }
        this.write(send);
    }

    /**
     * 将字符串形式表示的十六进制数转换为byte数组
     */
    private byte[] hexStringToBytes(String hexString) {
        hexString = hexString.toLowerCase();
        String[] hexStrings = hexString.split(" ");
        byte[] bytes = new byte[hexStrings.length];
        for (int i = 0; i < hexStrings.length; i++) {
            char[] hexChars = hexStrings[i].toCharArray();
            bytes[i] = (byte) (charToByte(hexChars[0]) << 4 | charToByte(hexChars[1]));
        }
        return bytes;
    }

    private byte charToByte(char c) {
        return (byte) "0123456789abcdef".indexOf(c);
    }

    /**
     * 打印纸一行最大的字节
     */
    private static final int LINE_BYTE_SIZE = 32;

    /**
     * 打印三列时，中间一列的中心线距离打印纸左侧的距离
     */
    private static final int LEFT_LENGTH = 16;
    /**
     * 打印三列时，中间一列的中心线距离打印纸右侧的距离
     */
    private static final int RIGHT_LENGTH = 16;

    /**
     * 打印三列时，第一列汉字最多显示几个文字
     */
    private static final int LEFT_TEXT_MAX_LENGTH = 4;

    public static String printData(String leftText, String middleText1, String middleText2, String rightText) {
        StringBuilder sb = new StringBuilder();
        // 左边最多显示 LEFT_TEXT_MAX_LENGTH 个汉字 + 两个点
//        if (leftText.length() > LEFT_TEXT_MAX_LENGTH) {
//            rightText = rightText.substring(0, LEFT_TEXT_MAX_LENGTH) + "..";
//        }
        int leftTextLength = getBytesLength(leftText);
        int middleTextLength1 = getBytesLength(middleText1);
        int middleTextLength2 = getBytesLength(middleText2);
        int rightTextLength = getBytesLength(rightText);


        // 计算左侧文字和中间文字的空格长度
        int trim1 = 8 - leftTextLength;
        int trim2 = 8 - middleTextLength1;
        int trim3 = 12 - middleTextLength2;

        sb.append(leftText);
        for (int i = 0; i < trim1; i++) {
            sb.append(" ");
        }
        sb.append(middleText1);

        for (int i = 0; i < trim2; i++) {
            sb.append(" ");
        }
        sb.append(middleText2);

        for (int i = 0; i < trim3 - 1; i++) {
            sb.append(" ");
        }
        sb.append(rightText);

        // 打印的时候发现，最右边的文字总是偏右一个字符，所以需要删除一个空格
//        sb.delete(sb.length() - 1, sb.length()).append(rightText);
        return sb.toString();
    }

    public static String printTwoData(String leftText, String rightText) {
        StringBuilder sb = new StringBuilder();
        // 左边最多显示 LEFT_TEXT_MAX_LENGTH 个汉字 + 两个点
//        if (leftText.length() > LEFT_TEXT_MAX_LENGTH) {
//            rightText = rightText.substring(0, LEFT_TEXT_MAX_LENGTH) + "..";
//        }
        int leftTextLength = getBytesLength(leftText);


        // 计算左侧文字和中间文字的空格长度
        int trim = 16 - leftTextLength;

        sb.append(leftText);
        for (int i = 0; i < trim; i++) {
            sb.append(" ");
        }
        sb.append(rightText);

        // 打印的时候发现，最右边的文字总是偏右一个字符，所以需要删除一个空格
//        sb.delete(sb.length() - 1, sb.length()).append(rightText);
        return sb.toString();
    }

    public static String printOneData(String text) {
        StringBuilder sb = new StringBuilder();
        // 左边最多显示 LEFT_TEXT_MAX_LENGTH 个汉字 + 两个点
//        if (text.length() > LEFT_TEXT_MAX_LENGTH) {
//            text = text.substring(0, LEFT_TEXT_MAX_LENGTH) + "..";
//        }
        int textLength = getBytesLength(text);


        // 计算左侧文字和中间文字的空格长度
        int trim = 32 - textLength;

        for (int i = 0; i < trim; i++) {
            sb.append(" ");
        }
        sb.append(text);

        // 打印的时候发现，最右边的文字总是偏右一个字符，所以需要删除一个空格
//        sb.delete(sb.length() - 1, sb.length()).append(rightText);
        return sb.toString();
    }

    private static int getBytesLength(String msg) {
        return msg.getBytes(Charset.forName("GB2312")).length;
    }

}
